//
//  WYTaskController.m
//  Boss
//
//  Created by admin on 2018/6/28.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYTaskController.h"
#import "WYTaskCell.h"
#import "WYSendRedPacketController.h"
#import "WYShareController.h"
#import "WYVIPController.h"
#import "WYTaskModel.h"
#import "WYPersonHomePageController.h"
#import "WYTaskListModel.h"
#import "CJAuthenticationController.h"
#import "CJMYCompanyController.h"
#import "WYPersonMessageController.h"
@interface WYTaskController ()<UITableViewDelegate,UITableViewDataSource,WYTaskCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *nameArray;
@property (nonatomic,strong)NSArray *descArray;
@property (nonatomic,strong)WYTaskModel *taskModel;
@property (nonatomic,strong)NSArray *btnArray;
@property (nonatomic,strong)NSMutableArray *listArray;
@end

@implementation WYTaskController
-(NSMutableArray *)listArray
{
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc]init];
    }
    return _listArray;
}
-(NSArray *)btnArray
{
    if (!_btnArray) {
        _btnArray = @[@"发布红包",@"分享商机",@"邀请好友",@"完善信息"];
    }
    return _btnArray;
}
-(NSArray *)nameArray
{
    if (!_nameArray) {
        _nameArray =@[@"发布一条红包信息",@"分享任意商机信息",@"邀请好友下载APP并成功登录",@"完善个人主页信息",@"开通月度VIP会员",@"开通年度VIP会员"];
    }
    return _nameArray;
}
-(NSArray *)descArray
{
    if (!_descArray) {
        _descArray = @[@"+3次领取上限（可重复完成)",@"+1次领取上限（三次/日）",@"+3次领取上限（可重复完成）",@"+3次领取上限（限一次）",@"+3次领取上限及五元红包奖励（限当日）",@"当日领取无上限及10元红包奖励（限当日）"];
    }
    return _descArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self loadData];
    [self loadListData];

    
}
-(void)loadListData
{
    WEAKSELF
    [WYHttpServer getTaskListSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            [weakSelf.listArray  addObjectsFromArray:[WYTaskListModel getTaskListModelFromJson:response[@"result"]]];
            [weakSelf loadData];
          //  [weakSelf.tableView reloadData];
        }
        
        
    }];
    
    
    
    
    
    
}
-(void)loadData
{
    WEAKSELF
    [WYHttpServer getUserTaskSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            weakSelf.taskModel = [WYTaskModel getTaskModelFromJson:response[@"result"]];
            for (WYTaskListModel *model in weakSelf.listArray) {
                if ([model.equalName isEqualToString:@"redpacket"]) {
                    model.state = weakSelf.taskModel.redpacket;
                }
                if ([model.equalName isEqualToString:@"certification"]) {
                    model.state = weakSelf.taskModel.share;
                }
                if ([model.equalName isEqualToString:@"userInfo"]) {
                    model.state = weakSelf.taskModel.inviteFriends;
                }
                if ([model.equalName isEqualToString:@"firmInfo"]) {
                      model.state = weakSelf.taskModel.userInfo;
                }
            }
            
            [weakSelf.tableView reloadData];
        }else{
            [self toast:errorMessage];
        }
    }];
    
    
}


-(void)setupUI
{
    self.navigationItem.title = @"做任务";
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYTaskCell" bundle:nil] forCellReuseIdentifier:@"WYTaskCell"];
    self.tableView.rowHeight = 96;
    
    self.tableView.backgroundColor = [UIColor hexStringToColor:@"#FC9961" alpha:1.0f];
    
    //self.view.backgroundColor = [UIColor hexStringToColor:@"#FC9961" alpha:1.0];
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor hexStringToColor:@"#FC9961" alpha:1.0f];
    self.tableView.backgroundView = view;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYTaskCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYTaskCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYTaskCell" owner:nil options:nil].firstObject;
    }
    cell.backgroundColor = [UIColor hexStringToColor:@"#FF6C5A" alpha:1.0];
   // cell.titleNameLabel.text = self.nameArray[indexPath.row];
   // cell.descNameLabel.text = self.descArray[indexPath.row];
    cell.delegate = self;
    
    if (self.listArray.count) {
        WYTaskListModel *model = self.listArray[indexPath.row];
        [cell setModel:model];
    }
    
    
//    if (self.taskModel) {
//        if (indexPath.row==0) {
//            if (self.taskModel.redpacket.integerValue) {
//                [cell.doneBtn setTitle:@"已完成" forState:UIControlStateNormal];
//                [cell.doneBtn setBackgroundColor:[UIColor hexStringToColor:@"#FC9961" alpha:1.0]];
//                cell.doneBtn.enabled = NO;
//            }else{
//                [cell.doneBtn setTitle:@"发布红包" forState:UIControlStateNormal];
//                  [cell.doneBtn setBackgroundColor:[UIColor hexStringToColor:@"#d8593f" alpha:1.0]];
//                cell.doneBtn.userInteractionEnabled = YES;
//                cell.btnWidth.constant = 90;
//            }
//        }
//        if (indexPath.row==1) {
//            if (self.taskModel.share.integerValue) {
//                [cell.doneBtn setTitle:@"已完成" forState:UIControlStateNormal];
//                 [cell.doneBtn setBackgroundColor:[UIColor hexStringToColor:@"#FC9961" alpha:1.0]];
//                cell.doneBtn.enabled = NO;
//            }else{
//                [cell.doneBtn setTitle:@"分享商机" forState:UIControlStateNormal];
//                  [cell.doneBtn setBackgroundColor:[UIColor hexStringToColor:@"#d8593f" alpha:1.0]];
//                cell.doneBtn.userInteractionEnabled = YES;
//                cell.btnWidth.constant = 90;
//            }
//        }
//        if (indexPath.row==2) {
//            if (self.taskModel.inviteFriends.integerValue) {
//                [cell.doneBtn setTitle:@"已完成" forState:UIControlStateNormal];
//                 [cell.doneBtn setBackgroundColor:[UIColor hexStringToColor:@"#FC9961" alpha:1.0]];
//                cell.doneBtn.enabled = NO;
//            }else{
//                [cell.doneBtn setTitle:@"邀请好友" forState:UIControlStateNormal];
//                  [cell.doneBtn setBackgroundColor:[UIColor hexStringToColor:@"#d8593f" alpha:1.0]];
//                cell.doneBtn.userInteractionEnabled = YES;
//                cell.btnWidth.constant = 90;
//            }
//        }
//        if (indexPath.row==3) {
//            if (self.taskModel.userInfo.integerValue) {
//                [cell.doneBtn setTitle:@"已完成" forState:UIControlStateNormal];
//                 [cell.doneBtn setBackgroundColor:[UIColor hexStringToColor:@"#FC9961" alpha:1.0]];
//                cell.doneBtn.enabled = NO;
//            }else{
//                [cell.doneBtn setTitle:@"完善信息" forState:UIControlStateNormal];
//                  [cell.doneBtn setBackgroundColor:[UIColor hexStringToColor:@"#d8593f" alpha:1.0]];
//                cell.doneBtn.userInteractionEnabled = YES;
//                cell.btnWidth.constant = 90;
//            }
//        }
//        if (indexPath.row==4) {
//            if (self.taskModel.yearVip.integerValue) {
//                [cell.doneBtn setTitle:@"已完成" forState:UIControlStateNormal];
//                 [cell.doneBtn setBackgroundColor:[UIColor hexStringToColor:@"#FC9961" alpha:1.0]];
//                cell.doneBtn.enabled = NO;
//            }else{
//                [cell.doneBtn setTitle:@"去完成" forState:UIControlStateNormal];
//                  [cell.doneBtn setBackgroundColor:[UIColor hexStringToColor:@"#d8593f" alpha:1.0]];
//                cell.doneBtn.userInteractionEnabled = YES;
//            }
//        }
//    }
    
    
    
    
    
    [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    return cell;
}
-(void)clickDone:(WYTaskCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
     WYTaskListModel *model = self.listArray[indexPath.row];
    
    
    if ([model.equalName isEqualToString:@"redpacket"]) {
        WYSendRedPacketController *spc = [[WYSendRedPacketController alloc]init];
        [self.navigationController pushViewController:spc animated:YES];
    }
    if ([model.equalName isEqualToString:@"certification"]) {
//        [self.tabBarController setSelectedIndex:1];
//        self.tabBarController.tabBar.hidden = NO;
//        [self.navigationController popViewControllerAnimated:YES];
        CJAuthenticationController *avc = [[CJAuthenticationController alloc]init];
        [self.navigationController pushViewController:avc animated:YES];
       
    }
    if ([model.equalName isEqualToString:@"firmInfo"]) {
//        WYShareController *svc = [[WYShareController alloc]init];
//        [self.navigationController pushViewController:svc animated:YES];
        
        WYMyInfo *info = [WYStoreModule currentMyInfo];
        
        if (info.firmCertificationStatus.integerValue) {
            CJMYCompanyController *mcc = [[CJMYCompanyController alloc]init];
            [self.navigationController pushViewController:mcc animated:YES];
        }else{
            CJAuthenticationController *avc =[[CJAuthenticationController alloc]init];
            [self.navigationController pushViewController:avc animated:YES];
        }
        
//        CJMYCompanyController *mcc = [[CJMYCompanyController alloc]init];
//        [self.navigationController pushViewController:mcc animated:YES];
    }
    if ([model.equalName isEqualToString:@"userInfo"]) {
        WYPersonMessageController *vvc = [[WYPersonMessageController alloc]init];
        [self.navigationController pushViewController:vvc animated:YES];
    }
    
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 180;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"task_header_background"]];
    return imageView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 41;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"task_bottom_alpha"]];
    return imageView;
    
}





- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    
    }
    return _tableView;
}

@end
